package servlet;

import dao.CollegeDao;
import dao.EpidemicDao;
import dao.Imp.CollegeDaoImp;
import dao.Imp.EpidemicDaoImp;
import dao.Imp.UserDaoImp;
import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        String user=request.getParameter("user") ;
        String pwd=request.getParameter("pwd");
        Date date=new Date();
        int hour=date.getHours();
        if (hour>=22){
            response.sendRedirect("Time.html");
        }
        else{
            String myCheck=request.getParameter("mode");
            if(myCheck.equals("user")) {
                UserDao userDao = (UserDao) new UserDaoImp();
                boolean isUser=userDao.login(user,pwd);
                if (isUser){
                    response.sendRedirect("user.html");
                }else{
                    request.setAttribute("msg", "用户名或密码错误!");
                    request.getRequestDispatcher("/login.html").forward(request, response);
                }
            }
            else if(myCheck.equals("college")) {
                CollegeDao collegeDao = (CollegeDao) new CollegeDaoImp();
                String isUser= String.valueOf(collegeDao.login(user,pwd));
                request.setAttribute("xueyuan", isUser);
                request.getRequestDispatcher("college.html").forward(request,response);
            }
            else if(myCheck.equals("epidemic")){
                EpidemicDao epidemicDao = (EpidemicDao) new EpidemicDaoImp();
                boolean isUser=epidemicDao.login(user,pwd);
                if (isUser==true){
                    response.sendRedirect("epidemic.html");
                }else{
                    request.setAttribute("msg", "用户名或密码错误!");
                    request.getRequestDispatcher("/login.html").forward(request, response);
                }
            }

        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
